
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class LoginForm extends Form implements CommandListener, Runnable {

    private TextField txtUsername;
    private TextField txtPassword;
    private Command cmdLogin;
    private Command cmdBack;
    private HomeScreen homeScreen;

    public LoginForm(HomeScreen homeScreen) {
        super("Login");
        this.homeScreen = homeScreen;
        txtUsername = new TextField("Username", "baopx", 30, TextField.ANY);
        txtPassword = new TextField("Password", "khongco", 30, TextField.PASSWORD);
        this.append(txtUsername);
        this.append(txtPassword);
        cmdLogin = new Command("Login", Command.OK, 2);
        cmdBack = new Command("Back", Command.BACK, 2);
        this.addCommand(cmdLogin);
        this.setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdLogin) {
            if (!Check()) {
                Common.Client.Login(txtUsername.getString(), txtPassword.getString());
                new Thread(this).start();
            }
        } else if (c == cmdBack) {
            homeScreen.DisplayHomeMain();
        }
    }

    private boolean Check() {
        boolean isError = false;
        if (txtUsername.getString().equals("")) {
            this.append("Username exist");
            isError = true;
        }
        if (txtPassword.getString().equals("")) {
            this.append("Password not empty");
            isError = true;
        }
        return isError;
    }

    public void run() {
        while (true) {
            if (Common.UserId > 0) {
                homeScreen.DisplayHomeMain();
                break;
            } else if (Common.UserId == -1) {
                homeScreen.DisplayError(ErrorType.LoginnFails);
                break;
            }
            System.gc();
        }
    }
}
